home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3624 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.1 KB  |  83 lines

  1. Newsgroups: comp.lang.c
  2. Path: cwi.nl!dik
  3. From: dik@cwi.nl (Dik T. Winter)
  4. Subject: Re: Matrix Multiplication
  5. Message-ID: <DLyvL5.9uH@cwi.nl>
  6. Sender: news@cwi.nl (The Daily Dross)
  7. Nntp-Posting-Host: chrysant.cwi.nl
  8. Organization: CWI, Amsterdam
  9. References: <822792812snz@genesis.demon.co.uk> <DLwzv9.9wu@cwi.nl> <822923024snz@genesis.demon.co.uk>
  10. Date: Mon, 29 Jan 1996 23:51:05 GMT
  11.  
  12. In article <822923024snz@genesis.demon.co.uk> fred@genesis.demon.co.uk writes:
  13.  > Right. Since in my version c is only ever written to cache line reads
  14.  > are not necessarily an issue.
  15. { k innermost in c[i][j] += a[i][k] * b[k][j] .}
  16.  > are not necessarily an issue. It is also outside the innermost loop so
  17.  > (in your case where N=1000) a cache miss compared to a 1000 iteration
  18.  > loop (with 1000 potential cache misses for b) should be insignificant.
  19.  > Indeed the chance of being able to accumulate the result in the inner loop
  20.  > in a register is likely to be more significant.
  21. See below.
  22.  > 
  23.  > >Timings on an SGI with a 100 MHz MIPS R4k (in seconds with N = 1000, 10
  24.  > >multiplications):
  25. This was an error in my posting while the array sizes where 1000x1000
  26. the computations were performed on 100x100 sections (and this makes
  27. a difference).
  28.  > >i innermost             11.08
  29.  > >k innermost              5.92
  30.  > >k innermost + scalar     4.60
  31.  > >j innermost              2.59
  32.  > 
  33. [ try using temp for a[i][k] when j is the innermost loop.]
  34.  > since that is an optimisation the compiler definitely can't make for itself
  35.  > if it can't tell that a and c are distinct.
  36.  
  37. I just tried it (and also a temp for b[k][j] when i is innermost and it
  38. makes absolutely no noticeable difference (as expected).  While the
  39. compiler is not allowed to keep a[i][k] in a register, the value will
  40. be kept in cache so each load of a[i][k] is just a cache read (unless
  41. a and c overlap of course).
  42.  > 
  43.  > Actually this makes it clear why your version works fast - *everything* 
  44.  > in the inner loop caches well. The only disadvantage is that the c update
  45.  > requires an 'external' read-modify-write sequence. The performance of that
  46.  > will depend on how write caching is implemented.
  47.  
  48. There is no need for an 'external' read-modify-write sequence!  When an
  49. element of c is updated it just writes out to the cache.  When an element
  50. of a or b loads that for some reason overlaps with the c element the
  51. effect is just a cache hit.  The cache makes aliasing transparent.  It
  52. depends on the workings of the cache whether a write to the cache also
  53. results in a direct write to memory or whether that can be delayed until
  54. there is no bus activity (or required because of replacement strategy).
  55.  
  56. It is important to keep in mind that with increasing processor speeds
  57. a cache miss may result in a load taking several cycles (I know of one
  58. processor where a secondary cache miss results in a load of 157 cycles,
  59. compared to a 2 cycle load on a primary cache hit).
  60.  
  61. Using the j index innermost most likely gives best performance as it
  62. does best with trying to optimize the number of cache hits, and timing
  63. suggest that it is the most insensitive to variations of the spacing
  64. of elements.  While in some cases the "k innermost + scalar" performs
  65. spectaculary well (and I have yet to analyze why) there are many cases
  66. that it performs much worse.
  67.  
  68.  > Maybe C could do as well as FORTRAN in this case.
  69.  
  70. Oh, it can (and in many other cases as well).  But it depends a bit on
  71. processor architecture.  Caches help a lot to alleviate the aliasing
  72. problem, but caches ought to be large enough and on most processors
  73. they are just way too small.  Also the cache replacement strategy
  74. implemented, the cache-line size and the set-associativity of the
  75. cache make great differences.  But Fortran suffers as well here.  And
  76. were it will not fly is on vector-processors, on those there is nothing
  77. to help with possible aliasing.  (On the other hand, I just had a try
  78. on a Cray C-90 and I do not understand the timings there...  I have
  79. to analyze a bit more I think.)
  80. -- 
  81. dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924098
  82. home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/
  83.